<?php
namespace app\mall\logic;

use app\data\model\DataUser;
use app\data\service\MessageService;
use Endroid\QrCode\QrCode;
use think\facade\Db;

class userLogic{

    /**
     * 查看用户的某个字段
     * @param int $uuid 用户id
     * @param string $field 字段
     */
    public static function user_value(int $uuid ,string $field){
        return Db::table('data_user')
            ->where('id','=',$uuid)
            ->value($field);
    }

    /**
     * 查看用户的多个字段
     * @param int $uuid 用户id
     * @param string $field 字段
     */
    public static function user_find(int $uuid ,string $field){
        return Db::table('data_user')
            ->where('id','=',$uuid)
            ->field($field)
            ->find();
    }

    /**
     * 修改用户的某个字段
     * @param int $uuid 用户id
     * @param string $field 字段
     * @param int|string $value
     */
    public static function user_info_edit(int $uuid ,string $field ,$value):int{
        $filter_field = array('id');
        if (in_array($field,$filter_field)) return 0;
        return Db::table('data_user')
            ->where('id','=',$uuid)
            ->update(array($field => $value));
    }

    /**
     * 修改用户的多个字段
     * @param int $uuid 用户id
     * @param array $param
     * @return int
     */
    public static function apply_info_edits(int $uuid ,array $param):int{
        return Db::table('data_user')
            ->where('id','=',$uuid)
            ->update($param);
    }

    /**
     * 个人中心
     * @param int $uid 用户id
     * @return array
     */
    public function individual_center(int $uid):array{
        $user_info = self::user_find($uid,'id ,nickname ,headimg ,phone');
        $user_wallet = Db::table('one_user_wallet')
            ->where('u_id','=',$uid)
            ->field('money ,integral')
            ->find();
        $not_drawn = Db::view('one_tesco_record','id ,u_id')
            ->where('u_id','=',$uid)
            ->view('one_goods_lucky_draw','is_draw_a_lottery','one_tesco_record.goods_lucky_draw_id = one_goods_lucky_draw.id')
            ->where('is_draw_a_lottery','=',0)
            ->count();

        $couponCanUseCount = Db::table('one_user_coupon')
            ->where(['u_id'=>$uid,'is_use'=>0])
            ->count();

        return array(
            'id' => $user_info['id'],
            'nickname' => $user_info['nickname'],//昵称
            'headimg' => $user_info['headimg'],//头像
            'phone' => $user_info['phone'],//手机
            'money' => $user_wallet['money'],//金币余额
            'integral' => $user_wallet['integral'],//积分余额
            'not_drawn' => $not_drawn,//未开奖商品
            'can_use_coupon_count'=>$couponCanUseCount,
        );
    }

    /**
     * 修改密码
     * @param int $uid 用户id
     * @param string $old_password 旧密码
     * @param string $new_password 新密码
     * @param string $new_re_password 重复新密码
     * @return array
     */
    public function change_password(int $uid,string $old_password,string $new_password,string $new_re_password):array{
        $user_info = DataUser::find($uid);
        if ($user_info['password']!=md5($old_password)){
            return array('code'=>500,'msg'=>'原密码错误');
        }
        if ($new_password!=$new_re_password){
            return array('code'=>500,'msg'=>'新密码不一致');
        }

        $user_info->save(['password'=>md5($new_password)]);
        return array('code'=>200,'msg'=>'修改成功');
    }

    /**
     * 重置密码
     * @param array $user 用户
     * @param string $password 密码
     * @param string $code 验证码
     * @return array
     */
    public function reset_password(array $user, string $new_password, string $new_re_password, string $code): array
    {
        $phone       = $user['phone'];
        $commomLogic = new commonLogic();
        $is_password = $commomLogic->ispwd($new_password, 1);

        if (!$is_password) {
            return ['code' => 500, 'msg' => '登录密码6-12位'];
        }
        if ($new_password != $new_re_password) {
            return ['code' => 500, 'msg' => '两次密码不一致'];
        }
        $result = MessageService::instance()->checkVerifyCode($code, $phone);
        if (!$result) {
            return ['code' => 500, 'msg' => '验证码不正确'];
        }

        if (!isset($user['id'])) {
            $user = DataUser::where('phone', '=', $user['phone'])->find()->toArray();
        }

        self::apply_info_edits($user['id'], ['password' => md5($new_password)]);
        return ['code' => 200, 'msg' => '重置成功'];
    }

    /**
     * 重置支付密码
     * @param array $user 用户
     * @param string $new_pay_password 支付密码
     * @param string $new_re_pay_password 重复支付密码
     * @param string $code 验证码
     * @return array
     */
    public function reset_pay_password(array $user ,string $new_pay_password ,string $new_re_pay_password ,string $code):array{
        $phone = $user['phone'];
        $commomLogic = new commonLogic();
        $is_password = $commomLogic->ispwd($new_pay_password,2);
        if (!$is_password){
            return array('code'=>500,'msg'=>'支付密码6位数字');
        }
        if ($new_pay_password!=$new_re_pay_password) return array('code'=>500,'msg'=>'两次密码不一致');
        if ($code != cache($phone)) return array('code'=>500,'msg'=>'验证码不正确');
        self::apply_info_edits($user['id'],['pay_password'=>md5($new_pay_password)]);
        return array('code'=>200,'msg'=>'重置成功');
    }
    /**
     * 更新用户资料
     * @param int $uid 用户id
     * @param string $password 密码
     * @param string $code 验证码
     * @return array
     */
    public function update_info(int $uid ,string $headimg ,string $nickname):array{
        $user_info = DataUser::find($uid);
        if ($headimg){
            $user_info->headimg = $headimg;
        }
        if ($nickname){
            $user_info->nickname = $nickname;
        }

        $user_info->save();
        return array('code'=>200,'msg'=>'修改成功');
    }

    /**
     * 更换绑定手机号
     * @param int $uid 用户id
     * @param string $phone 手机号
     * @param string $code 验证码
     * @return array
     */
    public function change_phone(int $uid ,string $phone ,string $code):array{
        $is_phone = loginLogic::verification_phone($phone);
        if (!$is_phone) return array('code'=>500,'msg'=>'手机号格式不正确');
        if ($code != cache($phone)) return array('code'=>500,'msg'=>'验证码不正确');
        self::apply_info_edits($uid,['phone'=>$phone]);
        return array('code'=>200,'msg'=>'更换成功');
    }

    /**
     * 生成用户二维码
     * @param string $text 手机号
     * @return array
     */
    public function scerweima1(string $text):array{

        $pathname = public_path(). '/upload/qrcode/';

        if(!is_dir($pathname)) { //若目录不存在则创建之
            mkdir($pathname,0777,true);
        }
//        $text = env('host.h5url')."?phone=$text";
//        var_dump($text);
//        die;
        //生成二维码图片
        $abc = $text.'.png';
        $filename = $pathname.$abc;

        if (file_exists($filename)){
            return array('code'=>200,'msg'=>'请求成功','data'=>request()->domain().'/upload/qrcode/'.$abc);
        }
        $h5url = 'https://yunwanggou.946260.com/h5/#/pages/login/register';
//        $text = env('host.h5url')."?phone=$text";
        $text = $h5url."?phone=$text";
//        var_dump($text);
//        die;
        $qrCode = new QrCode();
        $qrCode
            ->setText($text)
            ->setSize(300)
            ->setPadding(10)
            ->setErrorCorrection('high')
            ->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0])
            ->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0])
            ->setLabel('')
            ->setLabelFontSize(16)
            ->setImageType(QrCode::IMAGE_TYPE_PNG);

        $qrCode->save($filename);

        return array('code'=>200,'msg'=>'请求成功','data'=>request()->domain().'/upload/qrcode/'.$abc);

    }

}